import 'package:flutter/material.dart';

import 'package:player_plugin/polyv_controller.dart';
import 'package:player_plugin/polyv_player.dart';
import 'package:player_plugin/polyv_video_plugin.dart';
import 'package:player_plugin/video_player_value.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ///控制器
  late PolyvController _polyvController;

  double progressValue = 0.5;
  String polyvConfigString =
      "Vrb95FiG1llcp5NULASgikPx68w9veV1PwixFUiwpxIuwPpalo1RhxPybWfE4WptHnm/b8f86n6NpBBd3vjzmzL1Tj6Vj1vG/mlrdXYhf8n4JbBt0szPg/cH0t/KRsbIYc/PBrKAkZlgoY8EQFRg3w==";
  String polyvKey = "*#%hls123456789.";
  String polyvIv = ".987654321slh%#*";
  late String dataSource;

  late DataSourceType sourceType;

  bool isFullscreen = false;
  VideoPlayerState? videoPlayerState;

  String defaultVideoUrl =
      'https://mpv.videocc.net/8f38fa3352/8/8f38fa335264f6cd7cdfa9a5f2ed7cb8_1.mp4';

  @override
  void initState() {
    super.initState();

    ///播放器配置
    PolyvVideoPlugin.playerConfig(
      config: polyvConfigString,
      aesKey: polyvKey,
      iv: polyvIv,
    );

    dataSource = defaultVideoUrl;
    sourceType = DataSourceType.videoUrl;
  }

  @override
  dispose() {
    super.dispose();
    _polyvController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: isFullscreen ? null : AppBar(title: const Text('demo')),
        body: Column(
          children: [
            PolyvPlayer(
              dataSource: dataSource,
              sourceType: sourceType,
              onInit: onInit,
              onTimeUpdate: (VideoPlayerValue value) {
                // debugPrint("onTimeUpdate");
              },
              onFullscreen: (bool fullscreen) {
                debugPrint("out Fullscreen : $fullscreen");
                setState(() {
                  isFullscreen = fullscreen;
                });
              },
              onPop: (VideoPlayerValue value) {
                Navigator.of(context).pop();
              },
              children: <Widget>[
                Builder(builder: (BuildContext context) {
                  videoPlayerState = PolyvPlayer.of(context);
                  return const SizedBox();
                }),
              ],
            ),
            TextButton(
              onPressed: () {
                dataSource = defaultVideoUrl;
                setState(
                  () {},
                );
              },
              child: const Text('defaultVideo'),
            ),
            TextButton(
              onPressed: () {
                dataSource =
                    'https://mpv.videocc.net/8f38fa3352/7/8f38fa335252dea652836243dd7e3f97_1.mp4';
                setState(
                  () {},
                );
              },
              child: const Text('changeVideo'),
            ),
          ],
        ),
      ),
    );
  }

  void onInit(PolyvController viewPlayerController) async {
    ///拿到控制器
    _polyvController = viewPlayerController;
  }
}
